import bb.cascades 1.0
import org.puzzlehead.enigma 1.0

Page {
    property alias menuSource: menuModelManager.menuSource
    property alias menuId: menuModelManager.menuId
    property alias title: titleBar.title
    titleBar: TitleBar {
        id: titleBar
    }
    Container {
        ListView {
            dataModel: menuModelManager.dataModel
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    StandardListItem {
                        title: ListItemData.name
                        description: ListItemData.description
                        imageSource: {
                            if ("icon" in ListItemData) {
                                return ListItemData.icon
                            } else if (ListItemData.type == "menu") {
                                return "asset:///images/icons/ca_folder.png"
                            } else if (ListItemData.type == "page") {
                                return "asset:///images/icons/ca_info.png"
                            } else if (ListItemData.type == "link") {
                                return "asset:///images/icons/ca_browser.png"
                            } else {
                                return "asset:///images/icons/ca_connection_failed.png"
                            }
                        }
                    }
                }            ]
            function itemType(data, indexPath) {
                return "item"
            }
            onTriggered: {
                var item = dataModel.data(indexPath)
                if (item.type == "menu") {
                    var menu = menuCD.createObject()
                    menu.title = item.name
                    menu.menuId = item.id
                    menu.menuSource = menuModelManager.menuSource
                    navPane.push(menu)
                } else if (item.type == "page") {
                    pageCD.source = item.source
                    navPane.push(pageCD.createObject())
                } else if (item.type == "link") {
                    invoker.invoke(item.url)
                }
            }
            attachedObjects: [
                ComponentDefinition {
                    id: menuCD
                    source: "asset:///MenuPage.qml"
                },
                ComponentDefinition {
                    id: pageCD
                },
                BrowserInvoker {
                    id: invoker
                }
            ]
        }
    }
    attachedObjects: [
        MenuModelManager {
            id: menuModelManager
        }
    ]
}
